import { Connection } from 'mongoose';
import { getInstance } from '../../db';
import { organizationSchema } from '../../schemas/organizationSchema';

// 新增机构
export async function addOrgService(params: IAnyObject):Promise<IAnyObject> {
  const db: Connection = await getInstance();

  const organizationModel = db.model('organizationModel', organizationSchema);

  const result = await organizationModel.create(params);

  return result;
}


// 删除机构信息
export const deleteOrgService = async (id: string) => {
  const db: Connection = await getInstance();
  const organizationModel = db.model('organizationModel', organizationSchema);

  const result = await organizationModel.remove({ _id: id });

  return result;
}

// 获取所有的机构信息
export const getOrgListService = async (uid = ''): Promise<IAnyObject> => {
  const db: Connection = await getInstance();
  const organizationModel = db.model('organizationModel', organizationSchema);

  // 参训条件
  const queryCondition = uid ? {
    uid: {
      $eq: uid,
    },
  } : {};

  // 懒得搞分页了，操
  const result = await organizationModel.find(queryCondition);

  return result;
}